package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //不同的用户有自己的购物车数据
        //首先进行对象装换 ShoppingCartDTO->ShoppingCart

        //判断当前加入到购物车中的商品是否已经存在
        //1、根据当前操作用户的id，查询到该用户的购物车，在看看购物车中是否有该商品
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //如果购物车中此时有该商品，那么就是update 更新number
        if (list != null && list.size() > 0) {
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        } else {
            //如果此时购物车中没有该商品，那么就是insert 插入一条数据
            //在这里也要分两种情况 如果是套餐或者菜品就要查套餐表中的数据来完善shoppingCart中没有的字段
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //本次添加到购物车的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //得到当前操作人的id
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.getByUserId(userId);
    }

    /**
     * 删除购物车中的一件商品
     * @param shoppingCartDTO
     */
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        //业务逻辑：如果当前商品有多个，先获取商品，将number-1即可，在更新update
        //如果当前商品只有一个那就是直接删除
        //当前商品是菜品还是套餐
        ShoppingCart shoppingCart = new ShoppingCart();
        //用户id
        Long userId = BaseContext.getCurrentId();
        //菜品id
        Long dishId = shoppingCartDTO.getDishId();
        //套餐id
        Long setmealId = shoppingCartDTO.getSetmealId();
        //当前是菜品 由dishId 和dishFlavor查询到当前菜品
        //属性拷贝
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //设置userId
        shoppingCart.setUserId(userId);
        //动态查询
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        ShoppingCart cart = list.get(0);
        Integer number = cart.getNumber();
        if (number > 1) {
            number--;
            shoppingCart.setNumber(number);
            shoppingCartMapper.update(shoppingCart);
        } else {
            shoppingCartMapper.delete(shoppingCartDTO);
        }


    }

    @Override
    public void deleteAll() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }
}
